package com.td.spring.beans.context;

import com.td.spring.beans.BeanDefinition;
import com.td.spring.beans.factory.AbstractBeanFactory;
import com.td.spring.beans.factory.AutowireCapableBeanFactory;
import com.td.spring.beans.io.ResourceLoader;
import com.td.spring.beans.xml.XmlBeanDefinitionReader;
import java.util.Map;

/**
 * @author td
 * @date 2018/6/5
 */
public class ClassPathXmlApplicationContext extends AbstractApplicationContext {

    private String configLocation;

    public ClassPathXmlApplicationContext(String configLocation) throws Exception {
        this(configLocation,new AutowireCapableBeanFactory());
    }

    public ClassPathXmlApplicationContext(String configLocation, AbstractBeanFactory beanFactory) throws Exception {
        super(beanFactory);
        this.configLocation = configLocation;
        refresh();
    }


    @Override
    protected void loadBeanDefinitions(AbstractBeanFactory beanFactory) throws Exception {

        XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(new ResourceLoader());
        xmlBeanDefinitionReader.loadBeanDefinitions(this.configLocation);
        for (Map.Entry<String, BeanDefinition> beanDefinitionEntry : xmlBeanDefinitionReader.getRegistry().entrySet()) {
           beanFactory.registerBeanDefinition(beanDefinitionEntry.getKey(),beanDefinitionEntry.getValue());
        }
    }


}
